<!DOCTYPE html>
<html lang="en">
<head>
    <meta charset="UTF-8"/>
    <meta http-equiv="X-UA-Compatible" content="IE=edge"/>
    <meta name="viewport" content="width=device-width, initial-scale=1"/>

    <link rel="stylesheet" type="text/css" href="/static/own/default.css">
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.7.0/themes/default/easyui.css">
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.7.0/themes/icon.css">
    <link rel="stylesheet" type="text/css" href="/static/jquery-easyui-1.7.0/demo/demo.css">
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/jquery.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/jquery.easyui.min.js"></script>
    <script type="text/javascript" src="/static/jquery-easyui-1.7.0/locale/easyui-lang-zh_CN.js"></script>
    <!--<script type="text/javascript" src='/static/easyui/js/outlook2.js'></script>-->
    <script type="text/javascript" src="/static/own/validateExtends.js"></script>
    <title>学生列表</title>
    <script type="text/javascript">
        $(function () {
            //datagrid初始化
            var dataList = $('#dataList');
            dataList.datagrid({
                title: '学生列表',
                iconCls: 'icon-more',//图标
                border: true,
                collapsible: false,//是否可折叠的
                fit: true,//自动大小
                method: "post",
                url: "/student/list",
                idField: 'id',
                singleSelect: false,//是否单选
                pagination: true,//分页控件
                rownumbers: true,//行号
                sortName: 'id',
                sortOrder: 'ASC',
                remoteSort: true,
                columns: [
                    [
                        {field: 'chk', checkbox: true, width: 50},
                        {field: 'id', title: 'ID', width: 50, sortable: true},
                        {field: 'account', title: '学号', width: 200, sortable: true},
                        {field: 'name', title: '姓名', width: 200},
                        {
                            field: 'clazz', title: '班级', width: 150,
                            formatter: function (value, row, index) {
                                if (row.clazz) {
                                    return row.clazz.name;
                                } else {
                                    return value;
                                }
                            }
                        },
                    ]
                ],
                toolbar: "#toolbar"
            });
            //设置分页控件
            var p = dataList.datagrid('getPager');
            $(p).pagination({
                pageSize: 10,//每页显示的记录条数，默认为10
                pageList: [10, 20, 30, 50, 100],//可以设置每页记录条数的列表
                beforePageText: '第',//页数文本框前显示的汉字
                afterPageText: '页    共 {pages} 页',
                displayMsg: '当前显示 {from} - {to} 条记录   共 {total} 条记录',
            });
            //设置工具类按钮
            $("#add").click(function () {
                $("#addDialog").dialog("open");
            });
            //修改
            $("#edit").click(function () {
                var selectRows = $("#dataList").datagrid("getSelections");
                if (selectRows.length !== 1) {
                    $.messager.alert("消息提醒", "请选择一条数据进行操作!", "warning");
                } else {
                    $("#editDialog").dialog("open");
                }
            });
            //删除
            $("#delete").click(function () {
                var selectRows = $("#dataList").datagrid("getSelections");
                var selectLength = selectRows.length;
                if (selectLength === 0) {
                    $.messager.alert("消息提醒", "请选择数据进行删除!", "warning");
                } else {
                    // var numbers = [];
                    // $(selectRows).each(function (i, row) {
                    //     numbers[i] = row.account;
                    // });
                    var ids = [];
                    $(selectRows).each(function (i, row) {
                        ids[i] = row.id;
                    });
                    $.messager.confirm("消息提醒", "将删除与学生相关的所有数据(包括成绩)，确认继续？", function (r) {
                        if (r) {
                            $.ajax({
                                type: "post",
                                url: "/student/delete",
                                data: {ids: ids},
                                success: function (msg) {
                                    if (msg === "success") {
                                        $.messager.alert("消息提醒", "删除成功!", "info");
                                        //刷新表格
                                        dataList.datagrid("reload");
                                        dataList.datagrid("uncheckAll");
                                    } else {
                                        $.messager.alert("消息提醒", "删除失败!", "warning");
                                        return;
                                    }
                                }
                            });
                        }
                    });
                }
            });

            //班级下拉框
            $("#clazzList").combobox({
                width: "150",
                height: "25",
                valueField: "id",
                textField: "name",
                multiple: false, //可多选
                editable: false, //不可编辑
                method: "post",
                url: "/clazz/all",
                onChange: function (newValue, oldValue) {
                    //加载班级下的学生
                    dataList.datagrid("options").queryParams = {clazzid: newValue};
                    dataList.datagrid("reload");
                }
            });

            //下拉框通用属性
            $("#add_clazzList, #edit_clazzList").combobox({
                width: "200",
                height: "30",
                valueField: "id",
                textField: "name",
                multiple: false, //可多选
                editable: false, //不可编辑
                method: "post",
            });

            $("#add_clazzList").combobox({
                url: "/clazz/all",
                onLoadSuccess: function () {
                    //默认选择第一条数据
                    var data = $(this).combobox("getData");
                    $(this).combobox("setValue", data[0].id);
                }
            });


            $("#edit_clazzList").combobox({
                url: "/clazz/all",
                onLoadSuccess: function () {
                    //默认选择第一条数据
                    var data = $(this).combobox("getData");
                    // data = data.rows;
                    if (data !== null)
                        $(this).combobox("setValue", data[0].id);
                }
            });

            //设置添加学生窗口
            $("#addDialog").dialog({
                title: "添加学生",
                width: 650,
                height: 460,
                iconCls: "icon-add",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                closable: true,
                buttons: [
                    {
                        text: '添加',
                        plain: true,
                        iconCls: 'icon-user_add',
                        handler: function () {
                            var addForm = $("#addForm");
                            var validate = addForm.form("validate");
                            if (!validate) {
                                $.messager.alert("消息提醒", "请检查你输入的数据!", "warning");
                                return;
                            } else {
                                var clazzid = $("#add_clazzList").combobox("getValue");
                                $.ajax({
                                    type: "post",
                                    url: "/student/add",
                                    data: addForm.serialize(),
                                    success: function (msg) {
                                        if (msg === "success") {
                                            $.messager.alert("消息提醒", "添加成功!", "info");
                                            //关闭窗口
                                            $("#addDialog").dialog("close");
                                            //清空原表格数据
                                            $("#add_name").textbox('setValue', "");

                                            //重新刷新页面数据
                                            dataList.datagrid("options").queryParams = {clazzid: clazzid};
                                            dataList.datagrid("reload");
                                            setTimeout(function () {
                                                $("#clazzList").combobox('setValue', clazzid);
                                            }, 100);

                                        } else {
                                            $.messager.alert("消息提醒", "添加失败!", "warning");
                                            return;
                                        }
                                    }
                                });
                            }
                        }
                    },
                    {
                        text: '重置',
                        plain: true,
                        iconCls: 'icon-reload',
                        handler: function () {
                            $("#add_name").textbox('setValue', "");
                        }
                    },
                ]
            });

            //设置编辑学生窗口
            $("#editDialog").dialog({
                title: "修改学生信息",
                width: 650,
                height: 460,
                iconCls: "icon-edit",
                modal: true,
                collapsible: false,
                minimizable: false,
                maximizable: false,
                draggable: true,
                closed: true,
                closable: true,
                buttons: [
                    {
                        text: '提交',
                        plain: true,
                        iconCls: 'icon-user_add',
                        handler: function () {
                            var validate = $("#editForm").form("validate");
                            var clazzid = $("#edit_clazzList").combobox("getValue");
                            if (!validate) {
                                $.messager.alert("消息提醒", "请检查你输入的数据!", "warning");
                                return;
                            } else {
                                $.ajax({
                                    type: "post",
                                    url: "/student/update",
                                    data: $("#editForm").serialize(),
                                    success: function (msg) {
                                        if (msg === "success") {
                                            $.messager.alert("消息提醒", "更新成功!", "info");
                                            //关闭窗口
                                            $("#editDialog").dialog("close");
                                            //刷新表格
                                            dataList.datagrid("options").queryParams = {clazzid: clazzid};
                                            dataList.datagrid("reload");
                                            dataList.datagrid("uncheckAll");

                                            setTimeout(function () {
                                                $("#clazzList").combobox('setValue', clazzid);
                                            }, 100);

                                        } else {
                                            $.messager.alert("消息提醒", "更新失败!", "warning");
                                            return;
                                        }
                                    }
                                });
                            }
                        }
                    },
                    {
                        text: '重置',
                        plain: true,
                        iconCls: 'icon-reload',
                        handler: function () {
                            //清空表单
                            $("#edit_name").textbox('setValue', "");
                        }
                    }
                ],
                onBeforeOpen: function () {
                    var selectRow = $("#dataList").datagrid("getSelected");
                    //设置值
                    var editId = $("#edit_id");
                    var editNumber = $("#edit_number");
                    var editName = $("#edit_name");
                    editId.textbox('setValue', selectRow.id);
                    editId.next().hide();
                    editNumber.textbox('setValue', selectRow.account);
                    editName.textbox('setValue', selectRow.name);
                    var clazzid = selectRow.clazz.id;
                    setTimeout(function () {
                        $("#edit_clazzList").combobox('setValue', clazzid);
                    }, 100);

                }
            });

        });
    </script>
</head>
<body>
<!-- 学生列表 -->
<table id="dataList" cellspacing="0" cellpadding="0">

</table>
<!-- 工具栏 -->
<div id="toolbar">
    <div style="float: left;"><a id="add" href="javascript:;" class="easyui-linkbutton"
                                 data-options="iconCls:'icon-add',plain:true">添加</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="edit" href="javascript:;" class="easyui-linkbutton"
                                 data-options="iconCls:'icon-edit',plain:true">修改</a></div>
    <div style="float: left;" class="datagrid-btn-separator"></div>
    <div style="float: left;"><a id="delete" href="javascript:;" class="easyui-linkbutton"
                                 data-options="iconCls:'icon-some-delete',plain:true">删除</a></div>

    <div style="margin-left: 10px;">班级：<input id="clazzList" class="easyui-textbox" name="clazz"/></div>

</div>

<!-- 添加学生窗口 -->
<div id="addDialog" style="padding: 10px">
    <form id="addForm" method="post">
        <table cellpadding="8">
            <tr>
                <td>姓名:</td>
                <td><input id="add_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text"
                           name="name" data-options="required:true, missingMessage:'请填写姓名'"/></td>
            </tr>
            <tr>
                <td>班级:</td>
                <td><input id="add_clazzList" style="width: 200px; height: 30px;" class="easyui-textbox"
                           name="clazz.id"/></td>
            </tr>
        </table>
    </form>
</div>

<!-- 修改学生窗口 -->
<div id="editDialog" style="padding: 10px">
    <form id="editForm" method="post">
        <table cellpadding="8">
            <input id="edit_id" class="easyui-textbox"
                   style="display:none" type="text" name="id"
            />

            <tr>
                <td>学号:</td>
                <td>
                    <input id="edit_number" class="easyui-textbox"
                           style="width: 200px; height: 30px;" type="text" name="account"
                           data-options="readonly: true, required:true"/>
                </td>
            </tr>
            <tr>
                <td>姓名:</td>
                <td><input id="edit_name" style="width: 200px; height: 30px;" class="easyui-textbox" type="text"
                           name="name" data-options="required:true, missingMessage:'请填写姓名'"/></td>
            </tr>
            <tr>
                <td>班级:</td>
                <td><input id="edit_clazzList" style="width: 200px; height: 30px;" class="easyui-textbox"
                           name="clazz.id"/></td>
            </tr>
        </table>
    </form>
</div>

</body>
</html>